import Roommgr from "./roommgr";


const userList: {[key: string]: SocketIO.Socket} = {};
let userOnline = 0;


function logErr(funName) {
    let pathFun = 'game_server->usermgr.ts->';
    return (errMsg) => {
        console.log(pathFun + funName + '()->错误信息:' + errMsg);
    };
}


export default class UserMgr{
    /**用用户的ID将用户的socket实例放入一个map中,并增加用户在线数量 */
    static bind(userId, socket){
        userList[userId] = socket;
        userOnline++;
    }


    /**检查用户是否在线 */
    static isOnline(userId){
        const socket = userList[userId];
        return socket ? true : false;
    }

    /**获取用户的socket实例 */
    static get(userId){
        return userList[userId];
    }

    /**删除用户的SOCKET实例，并减少在线人数 */
    static del(userId){
        delete userList[userId];
        userOnline--;
    }

    /**获取在线人数 */
    static getOnlineCount(){
        return userOnline;
    }
    

    /**
     * 给房间内的玩家发消息
     * @param event 事件名
     * @param data 发送的数据
     * @param sender 发送者
     * @param includingSender 是否给自己发消息
     */
    static broacastInRoom(event: game_event_map[keyof game_event_map], data, sender, includingSender = false){
        const roomId = Roommgr.getUserRoom(sender);
        if(roomId == null){
            console.log('错误信息： broacastInRoom()->roomId == null,事件名：' + event);
            return;
        }

        const roomInfo = Roommgr.getRoomInfo(roomId);
        if(roomInfo == null){
            console.log('错误信息： broacastInRoom()->roomInfo == null,事件名：' + event);
            return;
        }

        for(let i = 0; i < roomInfo.seats.length; i++){
            let rs = roomInfo.seats[i];
            if(rs.userId == sender && includingSender != true){
                continue;
            }

            let socket = userList[rs.userId];
            if(socket != null){
                socket.emit(event, data);
            }
        }
    }


    /**
     * 发送事件
     * @param userId 
     * @param event 
     * @param data 
     */
    static sendMsg(userId, event: game_event_map[keyof game_event_map], data?){
        const log = logErr('sendMsg');
        
        const socket = userList[userId];
        if(socket == null){
            log('socket == null');
            return;
        }

        socket.emit(event, data);
    }


    /**
     * 断开房间内玩家的socket连接
     * @param roomId 
     */
    static kickAllInRoom(roomId){
        const log = logErr('kickAllInRoom');

        if(roomId == null){
            log('roomId == null');
            return;
        }

        const roomInfo = Roommgr.getRoomInfo(roomId);
        if(roomInfo == null){
            log('roomInfo == null');
            return;
        }

        for(let i = 0; i < roomInfo.seats.length; i++){
            const rs = roomInfo.seats[i];
            // 如果不需要发送给发送方，则跳过
            if(rs.userId > 0){
                const socket = userList[rs.userId];
                if(socket != null){
                    this.del(rs.userId);
                    socket.disconnect();
                }
            }
        }
    }

}

